package com.ifast.data.controller;

import java.util.Arrays;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ifast.common.type.EnumErrorCode;
import com.ifast.common.utils.ImportExcelUtil;
import com.ifast.common.utils.Result;
import com.ifast.common.utils.StringUtils;
import com.ifast.data.domain.DataCityDO;
import com.ifast.data.service.DataCityService;

/**
 * 
 * <pre>
 * 
 * </pre>
 * 
 * <small> 2018-04-15 21:40:23 | lvyq</small>
 */
@Controller
@RequestMapping("/data/dataCity")
public class DataCityController {
	@Autowired
	private DataCityService dataCityService;

	@GetMapping()
	@RequiresPermissions("data:dataCity:dataCity")
	String DataCity() {
		return "data/dataCity/dataCity";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("data:dataCity:dataCity")
	public Result<Page<DataCityDO>> list(Integer pageNumber, Integer pageSize, DataCityDO dataCityDTO) {
		// 查询列表数据
		Page<DataCityDO> page = new Page<>(pageNumber, pageSize);

		Wrapper<DataCityDO> wrapper = new EntityWrapper<DataCityDO>(dataCityDTO);
		wrapper.orderBy("first_alphabet", true);
		wrapper.orderBy("is_popular", false);
		page = dataCityService.selectPage(page, wrapper);
		int total = dataCityService.selectCount(wrapper);
		page.setTotal(total);
		return Result.ok(page);
	}

	@GetMapping("/add")
	@RequiresPermissions("data:dataCity:add")
	String add() {
		return "data/dataCity/add";
	}

	@GetMapping("/edit/{cityId}")
	@RequiresPermissions("data:dataCity:edit")
	String edit(@PathVariable("cityId") Long cityId, Model model) {
		DataCityDO dataCity = dataCityService.selectById(cityId);
		model.addAttribute("dataCity", dataCity);
		return "data/dataCity/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("data:dataCity:add")
	public Result<String> save(DataCityDO dataCity) {
		dataCity.setCityId(dataCityService.getId() + 1);
		if (dataCityService.insert(dataCity)) {
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("data:dataCity:edit")
	public Result<String> update(DataCityDO dataCity) {
		dataCityService.updateAllColumnById(dataCity);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("data:dataCity:remove")
	public Result<String> remove(Long cityId) {
		if (dataCityService.deleteById(cityId)) {
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("data:dataCity:batchRemove")
	public Result<String> remove(@RequestParam("ids[]") Long[] cityIds) {
		dataCityService.deleteBatchIds(Arrays.asList(cityIds));
		return Result.ok();
	}

	@GetMapping("/api/v1/getCity")
	@ResponseBody
	public Result<List<DataCityDO>> getCity(DataCityDO dataCity) {
		Wrapper<DataCityDO> wrapper = new EntityWrapper<DataCityDO>(dataCity);
		wrapper.orderBy("first_alphabet", true);
		List<DataCityDO> Citys = dataCityService.selectList(wrapper);
		if (Citys != null && Citys.size() > 0) {
			return Result.ok(Citys);
		}
		return Result.ok();
	}

	@GetMapping("/api/v1/getCity2")
	@ResponseBody
	public JSONPObject getCity(String jsonpCallback, DataCityDO dataCity) {
		Wrapper<DataCityDO> wrapper = new EntityWrapper<DataCityDO>(dataCity);
		wrapper.orderBy("first_alphabet", true);
		wrapper.orderBy("is_popular", false);
		List<DataCityDO> Citys = dataCityService.selectList(wrapper);
		if (Citys != null && Citys.size() > 0) {
			return new JSONPObject(jsonpCallback, Result.ok(Citys));
		}
		return new JSONPObject(jsonpCallback, Result.ok());
	}

	@GetMapping("/api/v1/getDepartureAddress")
	@ResponseBody
	public Result<List<DataCityDO>> getDepartureAddress(String departure) {
		List<DataCityDO> Citys = dataCityService.getDepartureAddress(departure);
		if (Citys != null && Citys.size() > 0) {
			return Result.ok(Citys);
		}
		return Result.ok();
	}

	@GetMapping("/api/v1/getDestinationAddress")
	@ResponseBody
	public Result<List<DataCityDO>> getDestinationAddress(String destination) {
		List<DataCityDO> Citys = dataCityService.getDestinationAddress(destination);
		if (Citys != null && Citys.size() > 0) {
			return Result.ok(Citys);
		}
		return Result.ok();
	}

	@GetMapping("/api/v1/getDepartureAddress2")
	@ResponseBody
	public JSONPObject getDepartureAddress(String jsonpCallback, String departure) {
		List<DataCityDO> Citys = dataCityService.getDepartureAddress(departure);
		if (Citys != null && Citys.size() > 0) {
			return new JSONPObject(jsonpCallback, Result.ok(Citys));
		}
		return new JSONPObject(jsonpCallback, Result.ok());
	}

	@GetMapping("/api/v1/getDestinationAddress2")
	@ResponseBody
	public JSONPObject getDestinationAddress(String jsonpCallback, String destination) {
		List<DataCityDO> Citys = dataCityService.getDestinationAddress(destination);
		if (Citys != null && Citys.size() > 0) {
			return new JSONPObject(jsonpCallback, Result.ok(Citys));
		}
		return new JSONPObject(jsonpCallback, Result.ok());
	}

	@GetMapping("/batchImport")
	@RequiresPermissions("data:dataCity:batchImport")
	String batchImport() {
		return "data/dataCity/batchImport";
	}

	@ResponseBody
	@PostMapping("/uploadExcel")
	public Result<String> uploadExcel(@RequestParam("file") MultipartFile file) {
		try {
			List<List<Object>> list = new ImportExcelUtil().getBankListByExcel(file.getInputStream(),
					file.getOriginalFilename(), 1);
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
				DataCityDO dc = dataCityService.selectById(list.get(i).size()>=1?list.get(i).get(0) + "":"");
				if (dc != null) {
					dc.setFirstAlphabet(StringUtils.isNull(list.get(i).size()>=2?list.get(i).get(1) + "":""));
					dc.setCity(StringUtils.isNull(list.get(i).size()>=3?list.get(i).get(2) + "":""));
					dc.setDepartureAddress(StringUtils.isNull(list.get(i).size()>=4?list.get(i).get(3) + "":""));
					dc.setDepartureImg(StringUtils.isNull(list.get(i).size()>=5?list.get(i).get(4) + "":""));
					dc.setDestinationAddress(StringUtils.isNull(list.get(i).size()>=6?list.get(i).get(5) + "":""));
					dc.setDestinationImg(StringUtils.isNull(list.get(i).size()>=7?list.get(i).get(6) + "":""));
					dc.setIsPopular(Integer.parseInt(StringUtils.isNull(list.get(i).size()>=8?list.get(i).get(7)==null?"0":list.get(i).get(7)+"":"0")));
					dataCityService.updateAllColumnById(dc);
				} else {
					DataCityDO datacity = new DataCityDO();
					datacity.setCityId(Long.valueOf(StringUtils.isNull(list.get(i).size()>=1?list.get(i).get(0) + "":"")));
					datacity.setFirstAlphabet(StringUtils.isNull(list.get(i).size()>=2?list.get(i).get(1) + "":""));
					datacity.setCity(StringUtils.isNull(list.get(i).size()>=3?list.get(i).get(2) + "":""));
					datacity.setDepartureAddress(StringUtils.isNull(list.get(i).size()>=4?list.get(i).get(3) + "":""));
					datacity.setDepartureImg(StringUtils.isNull(list.get(i).size()>=5?list.get(i).get(4) + "":""));
					datacity.setDestinationAddress(StringUtils.isNull(list.get(i).size()>=6?list.get(i).get(5) + "":""));
					datacity.setDestinationImg(StringUtils.isNull(list.get(i).size()>=7?list.get(i).get(6) + "":""));
					datacity.setIsPopular(Integer.parseInt(StringUtils.isNull(list.get(i).size()>=8?list.get(i).get(7)==null?"0":list.get(i).get(7)+"":"0")));
					dataCityService.insert(datacity);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(EnumErrorCode.FileUploadGetBytesError.getCode(),
					EnumErrorCode.FileUploadGetBytesError.getMsg());
		}
		return Result.ok();
	}

}
